package au.com.twosquared.animalfarm.entities.animals.bird;

import java.util.ArrayList;

import au.com.twosquared.animalfarm.entities.Entity;
import au.com.twosquared.animalfarm.entities.animals.model.Animal;
import au.com.twosquared.animalfarm.map.Map;
import au.com.twosquared.animalfarm.sprites.Animation;
import au.com.twosquared.animalfarm.sprites.Image;
import au.com.twosquared.tools.Random;

/**
 * Defines the Chicken animal's interaction with the map along with animations
 * 
 * @author Jan Martin <jan.martin@twosquared.com.au>
 * @author Sam Luc <sam.luc@twosquared.com.au>
 * @version 21-11-2011
 * @since 20-11-2011
 * 
 */
public class Chicken extends Animal {

	// Put chicken related variables here (number of eggs, when to walk etc)
	private long animationTime;
	private boolean idle;

	/**
	 * Loads the chicken's animation and animation parameters
	 * 
	 */
	public Chicken(ArrayList<Animation> animations) {
		super(animations);

		// State must be set to anything but -1, -1 will not show the animal
		state = Random.integer(8);
		animationTime = Random.integer(2000);
		idle = true;

		width = 48;
		height = 48;
	}

	/**
	 * Logic defines the AI of the Chicken with random parameters and an idle
	 * animation between each other animation.
	 * 
	 */
	public void logic(long delta) {
		// Walks if on walking animation
		if (state == 6) {
			direction = 0;
			xs = 1;
		} else if (state == 7) {
			direction = 1;
			xs = -1;
		} else {
			xs = 0;
		}
		// Random animation timer
		animationTime -= delta;
		// Sets idling and random animation after idle

		if (animationTime <= 0) {
			if (idle) {
				idle = false;
				setState(Random.integer(6, 8));
			} else {
				idle = true;
				if (direction == 0) {
					setState(Random.integer(3));
				} else {
					setState(Random.integer(3, 6));
				}
			}
			if (state == 0 || state == 3) {
				animationTime = 2100 * Random.integer(1, 3);
			} else if (state == 1 || state == 4) {
				animationTime = 1400;
			} else if (state == 2 || state == 5) {
				animationTime = 2200;
			} else if (state == 6 || state == 7) {
				animationTime = 600 * Random.integer(1, 4);
			}
		}
	}

	// Remember, these collision functions DO NOT check for collision for you,
	// you have to do your own for now
	/**
	 * Checks for collision between the Chicken and the map borders
	 */
	public void collision(Map map) {
		if (map.getTile(x + width, y) == 1) {
			direction = 1;
			setState(7);
		} else if (map.getTile(x, y) == 1) {
			direction = 0;
			setState(6);
			ys = 0;
			
		}
		if (map.getTile(x, (y+height)) == 1){
			ys = 0;
		}
	}

	public void collision(Entity entity) {
		if(collides(entity)) {
			xs = -xs;
		}
	}

}
